package com.clb.studyproject.ui

import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.SeekBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.clb.studyproject.R
import com.clb.studyproject.weights.RoundCornerImageView
import java.net.URL

class RoundCornerImageActivity : AppCompatActivity(R.layout.activity_round_corner_imager) {

    private val TAG = javaClass.name
    private lateinit var imgShow: RoundCornerImageView
    private val picUrl = "https://img1.baidu.com/it/u=3102336817,2216289337&fm=26&fmt=auto&gp=0.jpg"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        imgShow = findViewById(R.id.img_show)
        Thread {
            val url = URL(picUrl)
            val openStream = url.openStream()
            val bitmap = BitmapFactory.decodeStream(openStream)
            Log.d(TAG, "onCreate: sss")
            runOnUiThread {
                imgShow.setImageBitmap(bitmap)
            }
        }.start()

        findViewById<SeekBar>(R.id.corner_value)?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                changeImgCorner(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }

        })
    }

    private fun changeImgCorner(progress: Int) {
        findViewById<TextView>(R.id.corner_value_show)?.text = "当前圆角值：$progress"
        imgShow.setRoundCorner(progress.toFloat())
    }
}
